home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / globe103.zip / GLOBE.C < prev    next >
C/C++ Source or Header  |  1990-10-07  |  23KB  |  813 lines

  1. /*    globe - display 3D vectors partly hidden by a sphere
  2.  
  3.     history...
  4.  
  5.         6 Oct 90    1.03: ported to Turbo C.  Calling scr_ci rather than 
  6.                     getchar.  Frame around display is off by default.
  7.                     '+' and '-' change amount by which scale changes.
  8.         4 Nov 88    1.02: added -ll option for longitude-latitude input
  9.         17 Jun 88    1.01: improved help page, fixed index bug, general
  10.                     code cleanup.
  11.         1 Jun 88    Adapted from DOTS
  12.  
  13.     bugs...
  14.         correct the hidden line elimination to account for these cases...
  15.  
  16.         o---\-------/---o   ->     o---\       /---o    
  17.              \_____/                    \_____/        
  18.  
  19.         o---\---o   /       ->     o---\       /        
  20.              \_____/                    \_____/        
  21.  
  22.         GLOBE should have the same flexible memory handling as GRAPH.
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "g3.h"
  30. #include "g.h"
  31. #include "scr_ci.h"
  32.  
  33. #define VERSION "1.03"
  34. #define RIGHT "Copyright (c) 1985, 1990 by James R. Van Zandt.  All rights reserved.\n"
  35. #define RESTRICTION "This program may be reproduced for personal, non-profit use only.\n"
  36.  
  37. #define PI 3.14159265358979
  38. #define d2r (PI/180.)
  39. #define r2d (180./PI)
  40. #define ENTRIES 3000
  41. #define MAXLAB 200
  42. #define MAXSTYLES 50
  43. #define BUFSIZE 200
  44.  
  45. extern double best_width, best_height;
  46.  
  47. double radius=1.;            /* sphere radius */
  48. double radius2=.999;        /* square of sphere radius, minus a bit */
  49. extern int debugging;
  50. int latlong = 0;
  51. char string[80];
  52. static char buf[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE];
  53. char *default_script_file = "";
  54. char *program_name = NULL;
  55. int style[MAXSTYLES];        /* array of requested line styles    */
  56. int repeat[MAXSTYLES];        /* array of repeat counts for line styles    */
  57. int persp=0,    /* nonzero if perspective projection desired */
  58. markers=0,        /* nonzero if marker desired at each data point */
  59. framing=0;        /* nonzero if frame desired around plot */
  60. int tails=0;
  61. int observer_viewpoint=0;
  62. int scaling=0;    /* nonzero if scaling data to fill a cube */
  63. int breaking=0;                /* nonzero if breaking (disconnecting) graph
  64.                                 after each label in input */
  65. int automatic_abscissas=0;    /* nonzero if abscissas are to be generated */
  66. int abscissa_arguments=0;    /* number specified: abscissa spacing & start */
  67. int logx=0;                    /* nonzero if x axis is to be logrithmic */
  68. int logy=0;                    /* nonzero if y axis is to be logrithmic */
  69. int logz=0;                    /* nonzero if z axis is to be logrithmic */
  70.  
  71. int x_arguments=0;            /* number specified: xmin, xmax, xdel    */
  72. int y_arguments=0;            /* number specified: ymin, ymax, ydel    */
  73. int standard_input=0;        /* nonzero if data is coming from standard input */
  74. int labels=0;                /* number of user-supplied labels
  75.                                  (index into next two arrays) */
  76. int numstyles=0;            /* # linestyles specified by user */
  77. int p_data[MAXLAB]; char *p_text[MAXLAB];
  78. char null_label[]="";
  79.  
  80. static double cc, ss, angle, step, factor=1.,
  81.     enlarge=1.,            /* scale factor for display size */
  82.     growth=1.414,        /* factor for changing scale factor */
  83.     abscissa=0.,        /* default starting value for automatic abscissas */
  84.     abscissa_step=1.,    /* default step for automatic abscissas */
  85. p1=-3., p2=0., p3=0.,    /* observer location */
  86. ox, oy,                    /* screen coordinates of top left corner */
  87. diag,                    /* extent of viewable volume */
  88. data_size;                /* extent of data */
  89.  
  90. double rotate[3][3];
  91.  
  92. #define REAL float
  93.  
  94. REAL *x, *y, *z;
  95. double xmin, xmax, ymin, ymax, zmin, zmax;
  96. int last;
  97.  
  98. main(argc, argv) int argc; char **argv;
  99. {    int i, j;
  100.  
  101.     if(argc<2 || (argc>1 && streq(argv[1], "?"))) sample();
  102.     read_data(argc, argv);
  103.     
  104.     initialize_core(0,0,3);
  105.     select_view_surface(1);
  106.     initialize_view_surface(1);
  107.  
  108.     initialize_parameters(argc, argv);
  109.     while(1)
  110.         {if(!plotting_device)
  111.             new_frame();                /* clears the screen */
  112.         if(persp) perspective(p1, p2, p3);
  113.         else parallel(p1, p2, p3);
  114.         view_plane_normal(-p1, -p2, -p3);
  115.         create_temporary_segment(1); /* finds viewing transformation */
  116.         frame();            /* draw framework around image */
  117.         find_transformation(p1, p2, p3);
  118.         draw_sphere();
  119. #ifdef VIS
  120. {
  121. double u;
  122. FILE *dfile;
  123. dfile=fopen("err","a");
  124. #define TV(a,b,c) fprintf(dfile, "visible(%f,%f,%f)=%d\n", a, b, c, visible(a,b,c))
  125. for (u=-2.2; u<3.; u+=.25) {TV(0.,0.,u);}
  126. for (u=-2.2; u<3.; u+=.25) {TV(0.,u,0.);}
  127. for (u=-2.2; u<3.; u+=.25) {TV(u,0.,0.);}
  128. fclose(dfile);
  129. new_frame();                /* clears the screen */
  130. }
  131. #endif
  132.         image();            /* use drawing primitives to create image */
  133.         close_temporary_segment();    
  134.         if(plotting_device) quit();
  135.         update_parameters();
  136.         }
  137. }
  138.  
  139. initialize_parameters(argc, argv) int argc; char **argv;
  140. {/*    if(want("use right handed coordinate system? ")) */
  141.         coordinate_system_type(1);        /* default coordinates are LH */
  142.     ndc_space_2(best_width, best_height); /* enable use of all of screen */
  143.     viewport2(0., best_width, 0., best_height); /* viewport is all of screen */
  144.     /*    if(want("enable clipping? ")) */
  145.         clip_window(1);
  146.     view_up_3(0., 0., 1.);        /* vector in this direction appears
  147.                                        vertical in final view        */
  148.     view_reference_point((xmax+xmin)/2., (ymin+ymax)/2., (zmin+zmax)/2.);
  149.     window(-diag*best_width, diag*best_width, 
  150.            -diag*best_height, diag*best_height); /* use all of view surface */
  151.     angle=3.14159/12.;
  152.     ss=sin(angle); cc=cos(angle);
  153. }
  154.  
  155. update_parameters()
  156. {    int c;
  157.     static double t;
  158.  
  159.     while(1)
  160.         {c=scr_ci();
  161.         switch(c)
  162.             {case '+':    angle *= 4.; step *= 4.; factor *= 4.; 
  163.                         growth *= growth;
  164.                         growth *= growth;
  165.                         ss=sin(angle); cc=cos(angle);
  166.                         break;
  167.             case '-':    angle /= 4.; step /= 4.; factor /= 4.; 
  168.                         growth = sqrt(sqrt(growth));
  169.                         ss=sin(angle); cc=cos(angle);
  170.                         break;
  171.             case '?':    help();    return;
  172.             case 'q':
  173.             case 'Q':    quit();
  174.             default:
  175.                 {c &= 0xff;
  176.                 if((c==up_char)|(c==('E'-64))) {p3+=step; return;}
  177.                 else if((c==down_char)|(c==('X'-64))) {p3-=step; return;}
  178.                 else if((c==right_char)|(c==('D'-64)))
  179.                     {t=cc*p1-ss*p2; p2= ss*p1+cc*p2; p1=t; return;
  180.                     }
  181.                 else if((c==left_char)|(c==('S'-64)))
  182.                     {t=cc*p1+ss*p2; p2=-ss*p1+cc*p2; p1=t; return;
  183.                     }
  184.                 else if(c == pgup_char)
  185.                     {if(!persp) goto BIGGER;
  186.                     p1/=growth; p2/=growth; p3/=growth; step/=growth; return;
  187.                     }
  188.                 else if(c == pgdn_char)
  189.                     {if(!persp) goto SMALLER;
  190.                     p1*=growth; p2*=growth; p3*=growth; step*=growth; return;
  191.                     }
  192.                 else if(c == home_char)
  193.                     {
  194. BIGGER:                enlarge *= growth;
  195.                     diag = data_size/enlarge;
  196.                     window(-diag*best_width, diag*best_width, 
  197.                            -diag*best_height, diag*best_height);
  198.                     ox=-diag*best_width; oy=diag*best_height*.95;
  199.                     return;
  200.                     }
  201.                 else if(c == end_char)
  202.                     {
  203. SMALLER:            enlarge /= growth;
  204.                     diag = data_size/enlarge;
  205.                     window(-diag*best_width, diag*best_width, 
  206.                            -diag*best_height, diag*best_height);
  207.                     ox=-diag*best_width; oy=diag*best_height*.95;
  208.                     return;
  209.                     }
  210.                         
  211. {
  212. FILE *dfile;
  213. dfile = fopen("debug","a");
  214. fprintf(dfile, "illegal character %d = %02xH\n", c, c);
  215. fclose(dfile);
  216. }                
  217.                 putchar(7);  /* beep */
  218.                 break;
  219.                 }
  220.             }
  221.         puts("\008 \008");    /* erase that character */
  222.         }
  223. }
  224.  
  225. quit()
  226. {
  227.     terminate_view_surface(1); 
  228.     printf("last viewpoint was (%f, %f, %f),\n", p1, p2, p3);
  229.     printf("scale factor %f\n", enlarge);
  230.     exit(0);
  231. }
  232.  
  233. char *msg[]={
  234. "            --- Status ---",
  235. "",
  236. buf,
  237. buf2,
  238. buf3,
  239. "",
  240. "",
  241. "            --- Key Commands ---",
  242. "",
  243. "cursor keys     move observer along a cylinder: higher, ",
  244. "                lower, around to left, or around to right.",
  245. "   pg up        move closer",
  246. "   pg dn        move back",
  247. "    home        enlarge",
  248. "    end         shrink",
  249. "     +          enlarge cursor key steps by a factor of 4",
  250. "     -          shrink cursor key steps by a factor of 4",
  251. "     Q          exit program",
  252. "",
  253. "     ?          print this help menu",
  254. "",
  255. "",
  256. "   <press any key to continue>",
  257. 0
  258. };
  259.  
  260. help()
  261. {    char **sp, c;
  262.     double x0, y0, z0, dz;
  263.     x0 = (xmax+xmin)/2.-diag*best_width;
  264.     z0 = (zmax+zmin)/2.+diag*best_height*.7;
  265.     dz = diag/24.;
  266.     new_frame();
  267.     parallel(0., -1.,